package com.tdkj.action.backstage.website;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
import org.omg.PortableInterceptor.SYSTEM_EXCEPTION;

import oracle.sql.TIMESTAMP;

import com.tdkj.action.BaseAction;
import com.tdkj.model.information.BeanBaseInfo;
import com.tdkj.model.information.BeanExpert;
import com.tdkj.model.user.BeanAdmin;
import com.tdkj.model.user.BeanUser;
import com.tdkj.model.website.BeanProtocolDetail;
import com.tdkj.model.website.BeanWebsiteNotice;
import com.tdkj.service.user.IBeanAdminService;
import com.tdkj.service.website.IBeanWebsiteNoticeService;
public class WebsiteNoticeAction  extends BaseAction{
	private Long id;
	private String title;
	private String content;
	private BeanWebsiteNotice notice;
	private List<BeanWebsiteNotice> notices=new ArrayList<BeanWebsiteNotice>();
	private IBeanWebsiteNoticeService websiteNoticeService;
	private IBeanAdminService adminService;
	private String[] ids;
	public String deleteAll() {
		if (null != ids) {
			for (String idtemp : ids) {
				this.getBaseService().delete(Long.parseLong(idtemp), BeanWebsiteNotice.class);
			}
		}
		return findAll();
	}
	public String[] getIds() {
		return ids;
	}

	public void setIds(String[] ids) {
		this.ids = ids;
	}
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public BeanWebsiteNotice getNotice() {
		return notice;
	}
	public void setNotice(BeanWebsiteNotice notice) {
		this.notice = notice;
	}
	public List<BeanWebsiteNotice> getNotices() {
		return notices;
	}
	public void setNotices(List<BeanWebsiteNotice> notices) {
		this.notices = notices;
	}
	public IBeanWebsiteNoticeService getWebsiteNoticeService() {
		return websiteNoticeService;
	}
	@Resource
	public void setWebsiteNoticeService(
			IBeanWebsiteNoticeService websiteNoticeService) {
		this.websiteNoticeService = websiteNoticeService;
	}
	public IBeanAdminService getAdminService() {
		return adminService;
	}
	@Resource
	public void setAdminService(IBeanAdminService adminService) {
		this.adminService = adminService;
	}
	
	public String findAll(){
		notices=websiteNoticeService.findAll(BeanWebsiteNotice.class);
		if(notices.size()==0)appendObj(20);
		Criteria criteria = this.createCriteria(BeanWebsiteNotice.class);
		criteria.addOrder(Order.desc("createtime"));// 对状态进行排序
		this.queryByPage(criteria, "websiteNoticeAction!findAll.action", 10);
		return "findAll";
	}
	public String findAddPage(){
		return "addPage";
	}
	public String findEditPage(){
		notice=websiteNoticeService.findById(this.getId(), BeanWebsiteNotice.class);
		return "editPage";
	}
	public String addWebsiteNotice(){
		BeanWebsiteNotice bean=new BeanWebsiteNotice();
		bean.setContent(this.getContent());
		bean.setTitle(this.getTitle());
		bean.setCreatetime(new Timestamp(System.currentTimeMillis()));
		BeanAdmin admin=websiteNoticeService.findById(1l, BeanAdmin.class);
		bean.setAdmin(admin);
		websiteNoticeService.save(bean);
		return findAll();
	}
	public String editWebsiteNotice(){
		BeanWebsiteNotice bean=websiteNoticeService.findById(this.getId(), BeanWebsiteNotice.class);
		bean.setContent(this.getContent());
		bean.setTitle(this.getTitle());
		websiteNoticeService.merge(bean);
		return findAll();
	}
	public String deleteWebsiteNotice(){
		websiteNoticeService.delete(this.getId(), BeanWebsiteNotice.class);
		return findAll();
	}
	private  void appendObj(int num) {
		// TODO Auto-generated method stub
		BeanWebsiteNotice bean=null;
		BeanAdmin admin=websiteNoticeService.findById(1l, BeanAdmin.class);
		for(int i=0;i<num;i++){
			bean=new BeanWebsiteNotice();
			bean.setTitle("好消息"+i);
			bean.setContent("公告内容"+i);
			bean.setAdmin(admin);
			bean.setCreatetime(new Timestamp(System.currentTimeMillis()));
			websiteNoticeService.save(bean);
		}
	}
	
}
